const CloudDBZoneWrapper = require("./CloudDBZoneWrapper.js");



module.exports.myHandler = async function(event, context, callback, logger) {

    var action;
    var data;

    const cloudDBZoneWrapper = new CloudDBZoneWrapper(logger);

    if (event.body) { 
        var _body = JSON.parse(event.body); 
        
        action = _body.action;
        data = _body.extraData;
    } else { 
        action = event.action;
        data = event.extraData;
    } 


    logger.info("data: " + JSON.stringify(data))
    let queryResult;
    switch(action) {
        case 'upsert':
            let upsertData = cloudDBZoneWrapper.getDataList(data);
            queryResult = cloudDBZoneWrapper.executeUpsert(upsertData);
            console.log(queryResult);
            break;

        case 'insert':
            let insertData = cloudDBZoneWrapper.getDataList(data);
            queryResult = cloudDBZoneWrapper.executeInsert(insertData);
            break; 

        case 'increment':
            queryResult = cloudDBZoneWrapper.executeIncrement(data);
            break;
        
        case 'update':
            queryResult = cloudDBZoneWrapper.executeUpdate(data);
            break;
            
        case 'delete':
            let deleteData = cloudDBZoneWrapper.getDataList(data);
            queryResult = cloudDBZoneWrapper.excuteDelete(deleteData);
            break;

        default:
            logger.info("invalid action");
            console.log("invalid action");

    }

    callback(queryResult);
};
